<template>
  <div class="mod-prize">
    <el-form :inline="true" :model="searchForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-select v-model="searchForm.activityId" clearable filterable placeholder="请选择活动">
          <el-option
            v-for="item in activityList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchForm.name" clearable placeholder="奖品名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('mall:prize:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      style="width: 100%;"
      @selection-change="selectionChangeHandle">
      <el-table-column
        align="center"
        header-align="center"
        label="奖品名称"
        prop="prizeName">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="活动名称"
        prop="activityName">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="奖品图片"
        prop="image">
        <template slot-scope="scope">
          <img v-if="scope.row.image" :src="scope.row.image" style="height: 30%;width: 30%"
               @click="openImg(scope.row.image)"/>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="奖品库存"
        prop="number">
      </el-table-column>
      <el-table-column
        :formatter="(a,b,c)=> c == -1 ?'只做占位，不常与实际抽奖':c == 1?'卡券':c == 4?'红包':'错误！'"
        align="center"
        header-align="center"
        label="奖品类型"
        prop="type">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="概率"
        prop="probability">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="排序"
        prop="sort">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="等级"
        prop="level">
      </el-table-column>
      <el-table-column
        align="center"
        fixed="right"
        header-align="center"
        label="操作"
        width="150">
        <template slot-scope="scope">
          <el-button v-if="isAuth('mall:prize:info')" size="small" type="text" @click="showDetails(scope.row.prizeId)">
            查看
          </el-button>
          <el-button v-if="isAuth('mall:prize:update')" size="small" type="text"
                     @click="addOrUpdateHandle(scope.row.prizeId)">修改
          </el-button>
          <el-button v-if="isAuth('mall:prize:delete')" size="small" type="text"
                     @click="deleteHandle(scope.row.prizeId)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="pageIndex"
      :page-size="pageSize"
      :page-sizes="[10, 20, 50, 100]"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './prize-add-or-update'

export default {
  data() {
    return {
      searchForm: {
        name: '',
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListSelections: [],
      activityList: [],
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate,
  },
  activated() {
    this.getDataList()
    this.getActivityData();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.$http({
        url: '/mall/prize/list',
        method: 'get',
        params: {
          'page': this.pageIndex,
          'limit': this.pageSize,
          'name': this.searchForm.name,
          'activityId': this.searchForm.activityId
        }
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.records
          this.totalPage = data.page.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 查看详情
    showDetails(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, true)
      })
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      let ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[删除]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: '/mall/prize/delete',
          method: 'post',
          data: ids
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500
            })
            this.getDataList()
          }
        })
      }).catch(() => {
      })
    },
    getActivityData() {
      this.$http({
        url: `/mall/activity/queryAll/`,
        method: 'get'
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.activityList = data.list
        }
      })
    },
  }
}
</script>
